package com.core.java.Generic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author: qilongxin
 * @date: 2021/11/5 16:34
 */
public class Generic {

    public static void main(String[] args) {
        // java泛型的实现原理是类型擦除，所有的类型信息在编译成字节码后都会被去掉
        List<Integer> inList = new ArrayList<>();
        List rList = inList;
        HashSet<? extends Boolean> set = new HashSet<>();
        List<Object> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<Number> list3 = new ArrayList<>();
        List<Double> list4 = new ArrayList<>();
        List<? extends Serializable> list5 = new ArrayList<>();
        List<? super Integer> list6 = new ArrayList<>();
        List<?> list7 = new ArrayList<>();


        // 类型通配符上限，限定集合的元素上限是 Number，元素都是 Number 的子类，只可从集合中取出元素
        ArrayList<Integer> integerList1 = new ArrayList<>();
        ArrayList<? extends Number> numberList1 = integerList1;
        Number number = numberList1.get(0);
        System.out.println(number);

        // 类型通配符下限，限定结合元素的下限是 Integer，元素都是 Integer 的父类，只可往集合中添加元素
        ArrayList<Number> numberList2 = new ArrayList<>();
        ArrayList<? super Integer> integerList2 = numberList2;
        Object object = new Object();
        Integer integer = (Integer) object;
        integerList2.add(integer);
    }

}
